﻿<div style="width: 100%;">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <a class="mini-button" iconcls="icon-add" plain="true" onclick="addRoot()">插入根节点</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-add" plain="true" onclick="addLeaf()">插入子节点</a>
                    <span class="separator"></span>
                    <!-- <a class="mini-button" iconcls="icon-add" plain="true" onclick="addBefore()">插入节点前</a>
                    <a class="mini-button" iconcls="icon-add" plain="true" onclick="addAfter()">插入节点后</a>-->
                    <a class="mini-button" iconcls="icon-remove" plain="true" onclick="removeRow()">删除</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-reload" plain="true" onclick="reloadRow()">刷新</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-save" plain="true" onclick="saveData()">保存</a>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="mini-fit" style="height: 100%;">
    <div id="treegrid" class="mini-treegrid" style="width: 100%; height: 100%;"
        url="/SystemData/Company/GetAllCompanyList" showtreeicon="true" showtreelines="true"
        treecolumn="CName" idfield="CID" parentfield="FID" resultastree="false"
        expandonload="true" allowcelledit="true" allowcellselect="true" onpreload="onPreload">
        <div property="columns">
            <div type="indexcolumn"></div>
            <div field="CCode" width="100" headeralign="center">
                公司编码
                  <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="CName" name="CName" width="120" headeralign="center">
                公司名称
                  <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="MiniCode" width="60" headeralign="center">
                助记码
                <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="CLeader" width="100" headeralign="center">
                负责人
                 <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="DID" width="100" headeralign="center" renderer="onDIDRenderer">
                类型
                <input property="editor" class="mini-combobox" data="companyType" valuefield="DID" textfield="DValue" style="width: 100%;" />
            </div>
            <div field="Tel1" width="100" headeralign="center">
                联系电话1
                <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="Tel2" headeralign="center" width="100">
                联系电话2
                <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="Tel3" headeralign="center" width="100">
                联系电话3
                <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="Fax" headeralign="center" width="100">
                传真
                <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="IsValid" type="checkboxcolumn" headeralign="center" width="40px">有效</div>
            <div field="IsRoot" type="checkboxcolumn" headeralign="center" width="40px" readonly="true">根</div>
            <div field="IsLeaf" type="checkboxcolumn" headeralign="center" width="40px" readonly="true">叶子</div>
            <div field="CLevel" headeralign="center" width="40px">
                层级
            </div>
            <div field="Sort" headeralign="center" width="40px">
                排序
                 <input property="editor" class="mini-spinner" minivalue="1" style="width: 100%;" />
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();
    var tree = mini.get("treegrid");

    var companyType;

    function onPreload() {
        if (!companyType) {
            $.ajax({
                url: "/SystemData/Company/GetCompanyType",
                type: "post",
                dataType: "json",
                async: false,
                success: function (data) {
                    companyType = data;
                }
            });
        }
    }

    function onDIDRenderer(e) {
        for (var i = 0, l = companyType.length; i < l; i++) {
            if (e.value == companyType[i].DID) {
                return companyType[i].DValue;
            }
        }
        return "";
    }

    function addRoot() {
        var newNode = { IsValid: 1, IsRoot: 1, IsLeaf: 1, CLevel: 1, Sort: tree.getData().length + 1 };
        tree.addNode(newNode, "after");
        tree.beginEditCell(newNode, "CName");
    }

    function addLeaf() {
        var node = tree.getSelectedNode();
        if (!node) {
            $.tip({ text: "请选择一个节点", staytime: 1000 });
            return;
        }

        var newNode = { IsValid: 1, IsRoot: 0, IsLeaf: 1, CLevel: node.CLevel + 1, Sort: tree.getChildNodes(node).length + 1 };
        tree.addNode(newNode, "add", node);
        tree.updateNode(node, { IsLeaf: 0 });
    }

    function addAfter() {
        var node = tree.getSelectedNode();

        if (!node) {
            $.tip({ text: "请选择一个节点", staytime: 1000 });
            return;
        }

        var newNode = { IsValid: 1, IsRoot: node.IsRoot, IsLeaf: 1, CLevel: node.CLevel };
        tree.addNode(newNode, "after", node);
    }

    function addBefore() {
        var node = tree.getSelectedNode();

        if (!node) {
            $.tip({ text: "请选择一个节点", staytime: 1000 });
            return;
        }

        var newNode = { IsValid: 1, IsRoot: node.IsRoot, IsLeaf: 1, CLevel: node.CLevel };
        tree.addNode(newNode, "before", node);
    }

    function removeRow() {
        var node = tree.getSelectedNode();
        if (node) {
            var fnode = tree.getParentNode(node);//父节点
            tree.removeNode(node);
            if (fnode) {//存在父节点
                if (tree.getChildNodes(fnode).length > 0) {
                    //不作处理
                }
                else {
                    tree.updateNode(fnode, { IsLeaf: 1 });
                }
            }
        }
    }

    function reloadRow() {
        tree.reload();
    }

    function saveData() {
        tree.validate();
        if (tree.isValid() == false) {
            var error = tree.getCellErrors()[0];
            tree.beginEditCell(error.record, error.column);
            return;
        }

        var add = tree.getChanges("added");
        add = mini.encode(add);

        var edit = tree.getChanges("modified");
        edit = mini.encode(edit);

        var remove = tree.getChanges("removed");
        remove = mini.encode(remove);

        tree.loading("保存中，请稍后......");
        $.ajax({
            url: "/SystemData/Company/SaveData",
            data: { add: add, edit: edit, remove: remove },
            type: "post",
            success: function (text, flag, res) {
                tree.unmask();
                if (res.responseText == "") {
                    $.tip({ text: "保存成功", staytime: 1000 });
                    tree.accept();
                }
                else
                    top.mini.alert(res.responseText);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                top.mini.alert(errorThrown.responseText);
            }
        });
    }


</script>
